package com.edgclub.edg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.edgclub.edg.Constant;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.MineActivity;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;


public class FindHomeFragment extends GzBaseFragment implements View.OnClickListener{
	private String classname = "FindHomeFragment";
	private ViewPager mViewPager;
	private ArrayList<Fragment> fragmentList;

	private TextView tv_name1,tv_name2,tv_name3;
	private TextView tv_bottom1,tv_bottom2,tv_bottom3;

	private FindIndexFragment indexFragment;
	private FindVideoFragment videoFragment;
	private FindCartoonFragment cartoonFragment;

	private PopupWindow popWindow = null; // 菜单
	private Activity mActivity;
	private UserPrefUtils userPrefUtils;
	private ImageView imgv_unread , mine;
	private GlideUtils glideUtils = new GlideUtils();;


	public static FindHomeFragment newInstance(String text) {
		FindHomeFragment f = new FindHomeFragment();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = this.getActivity();
		return inflater.inflate(R.layout.fragment_find_home, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView(view);
	}

	public void initView(View view){
		tv_name1 = (TextView) view.findViewById(R.id.tv_name1);
		tv_name2 = (TextView) view.findViewById(R.id.tv_name2);
		tv_name3 = (TextView) view.findViewById(R.id.tv_name3);
		tv_bottom1= (TextView) view.findViewById(R.id.tv_bottom1);
		tv_bottom2 = (TextView) view.findViewById(R.id.tv_bottom2);
		tv_bottom3 = (TextView) view.findViewById(R.id.tv_bottom3);
		mine = (ImageView)view.findViewById(R.id.mine);
		mine.setOnClickListener(this);
		tv_name1.setOnClickListener(this);
		tv_name2.setOnClickListener(this);
		tv_name3.setOnClickListener(this);

		userPrefUtils = new UserPrefUtils(getActivity());
		glideUtils.Glidde_loadCropCircleImage(getActivity(),userPrefUtils.getUser().getLogo(),mine);
		mViewPager = (ViewPager)getView().findViewById(R.id.viewpager_edg);
		mViewPager.setOffscreenPageLimit(3);
		fragmentList = new ArrayList<Fragment>();
		indexFragment= new FindIndexFragment();
		videoFragment = new FindVideoFragment();
		cartoonFragment = new FindCartoonFragment();
		fragmentList.add(indexFragment);
		fragmentList.add(videoFragment);
		fragmentList.add(cartoonFragment);
		//给ViewPager设置适配器
		mViewPager.setAdapter(new MyFragmentPagerAdapter(this.getChildFragmentManager(), fragmentList));
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
		mViewPager.setCurrentItem(0);//设置当前显示标签页为第一页
		switchtab(tv_name1, tv_bottom1);


		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).register(this);
	}

	@Override
	public void onHiddenChanged(boolean hidd) {
		if (hidd) {

		} else {
			//相当于Fragment的onPause
			if(null!=mViewPager)
				mViewPager.setCurrentItem(mViewPager.getCurrentItem());
			switch (mViewPager.getCurrentItem()){
				case 0:
					switchtab(tv_name1,tv_bottom1);
					break;
				case 1:
					switchtab(tv_name2, tv_bottom2);
					break;
				case 2:
					switchtab(tv_name3, tv_bottom3);
					break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.tv_name1:
				switchtab(tv_name1, tv_bottom1);
				mViewPager.setCurrentItem(0);
				break;
			case R.id.tv_name2:
				switchtab(tv_name2,tv_bottom2);
				mViewPager.setCurrentItem(1);
				break;
			case R.id.tv_name3:
				switchtab(tv_name3,tv_bottom3);
				mViewPager.setCurrentItem(2);
				break;
			case R.id.mine:
				Intent intent3 = new Intent(getActivity(), MineActivity.class);
				startActivityForResult(intent3, Constant.USER_RESULT_ICON);
				MobclickAgent.onEvent(getActivity(),"EC_CLICK_MINE");
				break;
		}
	}


	public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int page) {
			// TODO Auto-generated method stub
			switch (page){
				case 0:
					switchtab(tv_name1,tv_bottom1);
					MobclickAgent.onEvent(getActivity(),"EC_FIND_RECOMMEND");
					break;
				case 1:
					switchtab(tv_name2, tv_bottom2);
					MobclickAgent.onEvent(getActivity(),"EC_FIND_VIDEO");
					break;
				case 2:
					switchtab(tv_name3, tv_bottom3);
					MobclickAgent.onEvent(getActivity(),"EC_FIND_COMIC");
					break;
			}
		}
	}

	private void switchtab(TextView tv_name, TextView tv_bottom) {
		tv_name1.setSelected(false);
		tv_name2.setSelected(false);
		tv_name3.setSelected(false);
		tv_bottom1.setVisibility(View.GONE);
		tv_bottom2.setVisibility(View.GONE);
		tv_bottom3.setVisibility(View.GONE);
		tv_name.setSelected(true);
		tv_bottom.setVisibility(View.VISIBLE);
	}


	class MyFragmentPagerAdapter extends FragmentPagerAdapter {
		ArrayList<Fragment> list;

		public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
			super(fm);
			this.list = list;
		}

		@Override
		public Fragment getItem(int position) {
			return list.get(position);
		}

		@Override
		public int getCount() {
			return list.size();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).unregister(this);
	}
	// 更新用户头像
	@Subscribe
	public void updateUserIocnEvent(HashMap<String, Object> map) {
		String event = (String) map.get("event");
		if (BroadCastEvent.USERICONUPLOAD.equals(event)) {
			User user = (User) map.get("user");
			glideUtils.Glidde_loadCropCircleImage(getActivity(),user.getLogo(),mine);
		}
	}
}

